package com.pulaien.shop.controller;

import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.core.domain.entity.SysUser;
import com.pulaien.common.utils.SecurityUtils;
import com.pulaien.system.domain.Customer;
import com.pulaien.system.mapper.CustomerMapper;
import com.pulaien.system.service.ICustomerService;
import com.pulaien.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 客户Controller
 *
 * @author pulaien
 */
@RestController
@RequestMapping("/shop/customer")
@Api(tags = "商城端客户管理")
public class ShopCustomerController extends BaseController {
    @Autowired
    private ICustomerService customerService;

    @Resource
    private ISysUserService userService;

    @ApiOperation("获取用户信息")
    @GetMapping(value = "/getCustomerInfo")
    public AjaxResult getCustomerInfo() {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        Long sysUserId = currentUser.getUserId();
        Customer record = customerService.getSysUserId(sysUserId);

        SysUser user = userService.selectUserById(sysUserId);
        record.setAvatar(user.getAvatar());

        return AjaxResult.success(record);
    }

    /**
     * 获取客户详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取客户详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(customerService.getCustomerByUserId(id));
    }

    /**
     * 获取首页信息
     *
     * @return 结果
     */
    @ApiOperation("获取首页信息")
    @GetMapping("/getPersonInfo")
    public AjaxResult getPersonInfo() {
        return this.customerService.getPersonInfo();
    }


    @ApiOperation(value = "个人中心保存信息")
    @PostMapping(value = "/updateInfo")
    public AjaxResult updateInfo(@RequestBody Customer customer) {

        return this.customerService.updateInfo(customer);
    }

}
